package com.example.timmy.myapplication.ui.activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.timmy.myapplication.R;

/**
 * ExoPlayer 播放器测试
 */
public class ExoPlayerActivity extends AppCompatActivity implements View.OnClickListener {

    //mediaPlayer对象
    private MediaPlayer mPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exoplayer);

        initView();
        initData();
    }

    private void initView() {
        findViewById(R.id.start_play).setOnClickListener(this);
    }

    private void initData() {
        //创建mediaplayer对象
        mPlayer = new MediaPlayer();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_play:
                startPlay();
                break;
        }
    }

    // 开始播放音乐
    private void startPlay() {
        mPlayer.reset();
        mPlayer = MediaPlayer.create(ExoPlayerActivity.this, R.raw.error_2);//重新设置要播放的音频
        mPlayer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mPlayer.isPlaying()) {
            mPlayer.stop();
        }

        mPlayer.release();
    }
}
